﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UlsStudio.Engine;

namespace UlsStudio.DesktopApp
{
    public partial class DirectoryBuilderForm : Form
    {
        public DirectoryBuilderForm()  { InitializeComponent(); }

        public DirectoryInfo BaseDir { get; set; }
        public UlsScanSettings Settings { get; set; }


        private void DirectoryBuilderForm_Load(object sender, EventArgs e)
        {
            lblAdding.Text = "Searching for logfiles...";
            if (this.BaseDir != null)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        void FoundFile(FileInfo f)
        {
            // Update GUI on mainthread
            if (InvokeRequired)
            {
                Invoke(new Action<FileInfo>(FoundFile), f);
                return;
            };

            // Do stuff on main thread
            lblAdding.Text = f.FullName;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            UlsScanSettings.FileFoundDelegate del = new UlsScanSettings.FileFoundDelegate(FoundFile);
            this.Settings = UlsScanSettings.BuildSettingsFromDirectory(this.BaseDir, true, del);
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
